package cn.hl.org.controller;

import cn.hl.basic.util.AjaxResult;
import cn.hl.basic.util.PageList;
import cn.hl.org.domain.Department;
import cn.hl.org.query.DepartmentQuery;
import cn.hl.org.service.IDepartmentService;
import cn.hl.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    /** 调service层 */
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 根据id查询部门，Get，url地址栏传参
     * http://localhost:8080/department/id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    /**
     * 查询所有部门信息，Get
     * http://localhost:8080/department
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * 根据id删除部门，Delete，url地址栏传参
     * http://localhost:8080/department/id
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 添加或者删除部门，Put，请求体传参
     * http://localhost:8080/department
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询，Post，请求体传参
     * @param departmentQuery
     * @return
     * http://localhost:8080/department
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 批量删除，Patch，请求体传参
     * @param ids
     * @return
     * http://localhost:8080/department
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "查询所有")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}
